// MegaDrawerAddress.cpp : implementation file
//

#include "stdafx.h"
#include "vc10.h"
#include "MegaDrawerAddress.h"
#include "../RMSLib/Registry.h"
#include "../GUI/Messages.h"

extern CVC10App theApp;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerAddress
#define IDC_LIST		1000
#define IDC_SELECT		1001
#define IDC_CONNECT		1002
#define IDC_RING		1003

#define STRING_PARAM	"%s %s %s %s"

CMegaDrawerAddress::CMegaDrawerAddress()
{
}

CMegaDrawerAddress::~CMegaDrawerAddress()
{
}

void CMegaDrawerAddress::SetBitmaps(UINT uiSkinSingle, UINT uiSkinDouble, UINT uiSkinSingleUn, UINT uiSkinDoubleUn)
{
	CMegaDrawer::SetBitmaps(uiSkinSingle, uiSkinDouble, uiSkinSingleUn, uiSkinDoubleUn);

	// create controls
	CRect rc(80, 10, 94, 24);
	m_List.SetBitmaps(IDB_ICON_WHITE, IDB_ICON_GRAY);
	m_List.Create(
		WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_BORDER|
		LBS_OWNERDRAWFIXED|LBS_HASSTRINGS|LBS_NOTIFY, rc, this, IDC_LIST);

	m_btnConnect.Create("", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rc, this, IDC_CONNECT);
	m_btnConnect.SetBitmaps(IDB_CONNECT_OUT, IDB_CONNECT_DOWN, IDB_CONNECT_OVER);
	m_btnConnect.SetBackgroundBmp(m_pSkinSingle);

	m_btnSelect.Create("", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rc, this, IDC_SELECT);
	m_btnSelect.SetBitmaps(IDB_SELECT_OUT, IDB_SELECT_DOWN, IDB_SELECT_OVER);
	m_btnSelect.SetBackgroundBmp(m_pSkinSingle);

	m_btnRing.Create("", WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, rc, this, IDC_RING);

	m_stCurSel.SetBitmap(IDB_ICON_GRAY);
	m_stCurSel.Create("", WS_CHILD|WS_VISIBLE|WS_BORDER, rc, this);

	// after control's creation finished - repostiion them
	RepositionControls();
}

void CMegaDrawerAddress::RepositionControls()
{
	// reposition controls
	if(m_bActive)
	{
		if(m_nSize == 1)
		{
			m_List.SetWindowPos(NULL, 3, 70, 322, 120, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_btnSelect.SetWindowPos(NULL, 8, 191, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_btnConnect.SetWindowPos(NULL, 236, 187, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_btnRing.SetWindowPos(NULL, 184, 195, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_stCurSel.SetWindowPos(NULL, 3, 20, 323, 23, SWP_NOZORDER|SWP_SHOWWINDOW);
		}
		else
		{
			m_List.SetWindowPos(NULL, 66, 42, 322, 70, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_btnSelect.SetWindowPos(NULL, 432, 10, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_btnConnect.SetWindowPos(NULL, 481, 62, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_btnRing.SetWindowPos(NULL, 430, 71, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_stCurSel.SetWindowPos(NULL, 66, 11, 322, 23, SWP_NOZORDER|SWP_SHOWWINDOW);
		}
	}
	else
	// if drawer unactive - hide all controls
	{
		m_List.ShowWindow(SW_HIDE);
		m_btnSelect.ShowWindow(SW_HIDE);
		m_btnConnect.ShowWindow(SW_HIDE);
		m_btnRing.ShowWindow(SW_HIDE);
		m_stCurSel.ShowWindow(SW_HIDE);
	}
}

void CMegaDrawerAddress::SetSites(map<CString, CSite> * pSites)
{
	m_pSites = pSites;

	// clear list
	m_stCurSel.SetCaption("");
	m_List.ResetContent();

	// refill list
	map<CString,CSite>::const_iterator s;
	map<CString,CBuilding>::const_iterator b;
	map<CString,CRoom>::const_iterator r;
	map<CString,CInfo>::const_iterator c;

	for(s = m_pSites->begin(); s != m_pSites->end(); s++)
	{
		for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
		{
			for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
			{
				for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
				{
					char name[1024];
					sprintf(name, STRING_PARAM, (LPCSTR) s->first, (LPCSTR) b->first, (LPCSTR) r->first, (LPCSTR) c->first);
					m_List.AddString(name);
				}
			}
		}
	}

	// retrieve last connection params from registry
	char str[1024]={0}, s1[1024]={0}, s2[1024]={0}, s3[1024]={0}, s4[1024]={0};
//	if(REGISTRY_LoadString(KEY_LAST_SITE_CONNECTED, str, 1024))
	if(REGISTRY_LoadString(KEY_ADDR, str, 1024))
		
	sscanf(str, STRING_PARAM, s1, s2, s3, s4);
	sprintf(str, STRING_PARAM, s1, s2, s3, s4);
	if(LB_ERR != m_List.SelectString(-1, str))
		m_stCurSel.SetCaption(str);

	int nRing = 0;
#ifdef EXPIRED1
#else
	nRing = REGISTRY_LoadInt(KEY_RING,1);
#endif
	if(nRing)
		m_btnRing.SetCheck(BST_CHECKED);
	else
		m_btnRing.SetCheck(BST_UNCHECKED);
}

void CMegaDrawerAddress::SelectCamera(char * pszCameraSign)
{
	int idx = m_List.FindStringExact(-1, pszCameraSign);
	m_List.SetCurSel(idx);
}


CString CMegaDrawerAddress::GetSelectedSite()
{
	return m_strSite;
}

CString CMegaDrawerAddress::GetSelectedBuilding()
{
	return m_strBuilding;
}

CString CMegaDrawerAddress::GetSelectedRoom()
{
	return m_strRoom;
}

CString CMegaDrawerAddress::GetSelectedCamera()
{
	return m_strCamera;
}

int CMegaDrawerAddress::GetRing()
{
	return (m_btnRing.GetCheck() == BST_CHECKED ? 1 : 0);
}

BEGIN_MESSAGE_MAP(CMegaDrawerAddress, CMegaDrawer)
	//{{AFX_MSG_MAP(CMegaDrawerAddress)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_LBN_DBLCLK(IDC_LIST, OnAddressDblClick)
	ON_BN_CLICKED(IDC_SELECT, OnSelectAsCurrent)
	ON_LBN_SELCHANGE(IDC_LIST, OnCameraChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerAddress message handlers
void CMegaDrawerAddress::OnCameraChange()
{
	int idx = m_List.GetCurSel();
	char buf[1024];
	m_List.GetText(idx, buf);
	// send message to parent
	GetParent()->SendMessage(
		WM_CAMERA_SELECTED, 
		(WPARAM) GetDlgCtrlID(), 
		(LPARAM) buf);
}

void CMegaDrawerAddress::OnConnect()
{
	int idx = m_List.GetCurSel();
	if(idx != LB_ERR)
	{
		CString str;
		m_List.GetText(idx, str);

		sscanf(str, STRING_PARAM, m_strSite, m_strBuilding, m_strRoom, m_strCamera);

		// store last connection params into registry
		// find full camera info to store it into register
		CSite site = m_pSites->find(m_strSite)->second;
		CBuilding building = site.m_Buildings.find(m_strBuilding)->second;
		CRoom room = building.m_Rooms.find(m_strRoom)->second;
		CInfo camera = room.m_Cameras.find(m_strCamera)->second;
		char pszRegString[4000];
		sprintf(
			pszRegString, 
			"%s %s %s %s %i %s %s %i %i %i %i %i %i",
			(LPCSTR) m_strSite, (LPCSTR) m_strBuilding, (LPCSTR) m_strRoom, (LPCSTR) m_strCamera, 
			camera.m_iID, camera.m_strPassword, camera.m_ip, camera.m_port,
			camera.m_iFrameRate, camera.m_iQualityRange, camera.m_iBitrate, 
			camera.m_iWidth, camera.m_iHeight);

		REGISTRY_SaveString(KEY_LAST_SITE_CONNECTED, pszRegString);
		REGISTRY_SaveInt(KEY_RING, GetRing());

		GetParent()->SendMessage(WM_CONNECT_SITE);
	}
}

void CMegaDrawerAddress::OnAddressDblClick()
{
	OnCameraChange();
	OnConnect();
}

void CMegaDrawerAddress::OnSelectAsCurrent()
{
	int idx = m_List.GetCurSel();
	// if something selected, store last connection params into registry
	if(idx != LB_ERR)
	{
		CString str;
		m_List.GetText(idx, str);

		// find full camera info to store it into register
		char pszSite[1024], pszBuilding[1024], pszRoom[1024], pszCamera[1024];
		sscanf(str, STRING_PARAM, pszSite, pszBuilding, pszRoom, pszCamera);
		CSite site = m_pSites->find(pszSite)->second;
		CBuilding building = site.m_Buildings.find(pszBuilding)->second;
		CRoom room = building.m_Rooms.find(pszRoom)->second;
		CInfo camera = room.m_Cameras.find(pszCamera)->second;

		char pszRegString[4000];
		sprintf(
			pszRegString, 
			"%s %s %s %s %i %s %s %i %i %i %i %i %i",
			pszSite, pszBuilding, pszRoom, pszCamera, 
			camera.m_iID, camera.m_strPassword, camera.m_ip, camera.m_port,
			camera.m_iFrameRate, camera.m_iQualityRange, camera.m_iBitrate, 
			camera.m_iWidth, camera.m_iHeight);

		REGISTRY_SaveString(KEY_ADDR, pszRegString);
		m_stCurSel.SetCaption(str);
	}
}
